package com.crm.customers.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.customers.dao.CustomerRecyclebinDao;
import com.crm.customers.model.CustomerInfo;
import com.crm.customers.model.CustomerRecyclebin;
import com.crm.customers.service.CustomerRecyclebinService;
import com.crm.customers.service.CustomerService;
import com.crm.im.core.model.Message;
import com.crm.sys.service.PushService;
import com.crm.user.service.UserService;
@Service("customerRecyclebinService")
public class CustomerRecyclebinServiceImpl implements CustomerRecyclebinService {

	@Autowired
	private CustomerRecyclebinDao customerRecyclebinDao;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private PushService pushService;
	@Autowired
	private UserService userService;
	
	@Override
	public List<CustomerRecyclebin> getCustomerRecyclebinList(CustomerRecyclebin cc,
			int pageNo, int pageSize) {
		cc.setStartsize((pageNo-1)*pageSize);
		cc.setEndsize(pageSize);
		return customerRecyclebinDao.getCustomerRecyclebinList(cc);
	}

	@Override
	public int saveCustomerRecyclebin(CustomerRecyclebin cc) {
		int result = 0;
	    if(cc.getId()!=null){
	    	CustomerRecyclebin ccObj = new CustomerRecyclebin();
    		ccObj.setId(cc.getId());
    		ccObj =  customerRecyclebinDao.getCustomerRecyclebinInfo(ccObj);
    		if(ccObj.getCusid()!=null){
		    	result = customerRecyclebinDao.updateCustomerRecyclebin(cc);
		    	String msg = "放入回收站被驳回，驳回理由:"+cc.getOpinion();
		    	//同意放入回收站
		    	if(result>0&&cc.getState()==1){
	    			CustomerInfo info = new CustomerInfo();
		    		info.setIsdel(0);
		    		info.setState(4);
		    		info.setUid(0L);
		    		info.setId(ccObj.getCusid());
		    		customerService.delCustomerInfo(info);
		    		msg = "审批通过已成功放入回收站";
	    		} 
				/*
				 * Message message = new Message(); message.setType("2");
				 * message.setContent(ccObj.getCustomerInfo().getCname()+msg);
				 * message.setReceiver(String.valueOf(ccObj.getCreateuser()));
				 * pushService.pushMsgToUser(message);
				 * userService.sendMsgToQQ(ccObj.getCreateuser(),
				 * ccObj.getCustomerInfo().getCname()+msg);
				 */
	    	}
	    }else{
	    	//申请了自动进入 删除状态
	    	cc.setState(0);
	    	cc.setCreateuser(cc.getUpdateuser());
	    	result = customerRecyclebinDao.insertCustomerRecyclebin(cc);
	    	//临时添加
	    	CustomerInfo info = new CustomerInfo();
    		info.setIsdel(1);
    		info.setId(cc.getCusid());
    		customerService.delCustomerInfo(info);
	    }
		return result;
	}

}
